﻿// <copyright file="BuildSubcatchments.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>BuildSubcatchments. class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  /// <summary>
  ///  Gets List of subcatchments linkIds
  /// </summary>
  public class BuildSubcatchments
  {
    // TODO: create overload for Build that takes a list of node IDs and builds the subcatchment for those nodes
    private List<Subcatchment> subcatchments = new List<Subcatchment>();
    private List<INode> nodeList;
    private INetwork network;
    
    /// <summary>
    /// Initializes a new instance of the <see cref="BuildSubcatchments" /> class.
    /// </summary>
    /// <param name="network">A network</param>
    public BuildSubcatchments(INetwork network)
    {
      // TODO: Complete member initialization
      this.network = network;
    }
    
    /// <summary>
    /// Builds a list of subcatchments given either a list of nodes or for the entire network
    /// </summary>
    /// <param name="nodeList">An optional list of INodes</param>
    /// <returns>A list of subcatchments</returns>
    public List<Subcatchment> Build(List<INode> nodeList = null)
    {
      this.nodeList = (nodeList == null) ? this.network.Nodes.ToList() : nodeList;
      this.AssembleSubcatchments();
      return this.subcatchments;
    }
        
    private void AssembleSubcatchments()
    {
      // TO-DO
      // verify unique subcatchment names
      foreach (Node aNode in this.nodeList)
      {
        int bldgarea = 0;
        int prkgarea = 0;

        if (aNode.UpstreamAreas != null)
        {
          foreach (IArea aArea in aNode.UpstreamAreas)
          {
            // each STRT area is a separate subcatchment
            if (aArea.AreaType == AreaType.STRT)
            {
              Subcatchment s = new Subcatchment();
              s.SubcatchmentName = "S" + aArea.ToString();
              s.AreaSqFt = aArea.AreaSqFt;
              s.AreaType = AreaType.STRT;
              s.Node = aNode;
              s.FlowLengthFt = aArea.FlowLengthFt;
              s.SlopeFtPerFt = aArea.SlopeFtPerFt;
              this.subcatchments.Add(s);
            }
            
            // sum all building areas directed to n
            if (aArea.AreaType == AreaType.BLDG)
            {
              bldgarea += aArea.Directors.Where(d => d.ToNode == aNode).Sum(d => d.AreaSqFt);
            }

            // sum all parking areas directed to n
            if (aArea.AreaType == AreaType.PRKG)
            {
              prkgarea += aArea.Directors.Where(d => d.ToNode == aNode).Sum(d => d.AreaSqFt);
            }
          }

          if (bldgarea != 0)
          {
            Subcatchment s = new Subcatchment();
            s.SubcatchmentName = "B_" + aNode.NodeName; 
            s.AreaSqFt = bldgarea;
            s.AreaType = AreaType.BLDG;
            s.Node = aNode;
            s.FlowLengthFt = 50;
            this.subcatchments.Add(s);
           //// subcatchments.Add(new Subcatchment() { AreaSqFt = bldgarea, AreaType = AreaTypes.BLDG, Node = aNode });
          }

          if (prkgarea != 0)
          {
            Subcatchment s = new Subcatchment();
            s.SubcatchmentName = "P_" + aNode.NodeName;
            s.AreaSqFt = prkgarea;
            s.AreaType = AreaType.PRKG;
            s.Node = aNode;
            s.FlowLengthFt = 50;
            this.subcatchments.Add(s);
          }
        } 
      }
    }      
  }
}
